package com.ruoyi.supervise.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.domain.BaseConstructionPermit;
import com.ruoyi.supervise.domain.ConPermitFile;
import com.ruoyi.supervise.domain.ConstructionPermit;
import com.ruoyi.supervise.domain.ConstructionPermitUnit;
import com.ruoyi.supervise.domain.ResponsiblePersonInfo;
import com.ruoyi.supervise.domain.SafetyMeasuresInfo;
import com.ruoyi.supervise.mapper.ConstructionPermitMapper;
import com.ruoyi.supervise.service.IConstructionPermitService;
import com.ruoyi.supervise.util.FieldStrUtils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: ConstructionPermitServiceImpl
 * @Description: 施工许可impl
 * @Author wtj
 * @DateTime 2020年3月27日 上午10:05:19
 */
@Service
@DataSource(value = DataSourceType.SLAVE)
public class ConstructionPermitServiceImpl implements IConstructionPermitService 
{
    @Autowired
    private ConstructionPermitMapper constructionPermitMapper;

	@Override
	public List<ConstructionPermit> selectConstructionPermitList(ConstructionPermit constructionPermit) {
		if(StringUtils.isEmpty(constructionPermit.getBeginReportTime()) && !StringUtils.isEmpty(constructionPermit.getEndReportTime())) {
			constructionPermit.setBeginReportTime("1970-01-01");
		}else if(!StringUtils.isEmpty(constructionPermit.getBeginReportTime()) && StringUtils.isEmpty(constructionPermit.getEndReportTime())) {
			constructionPermit.setEndReportTime(DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN));
		}
		List<ConstructionPermit> constructionPermitList=constructionPermitMapper.selectConstructionPermitList(constructionPermit);
		for(ConstructionPermit c:constructionPermitList) {
			c.setTotalCompanyGuid(FieldStrUtils.personOrEnterFieldParaNot(c.getTotalCompany(), 2, 0));
    		c.setTotalCompanyName(FieldStrUtils.personOrEnterFieldParaNot(c.getTotalCompany(), 2, 1));
		}
		return constructionPermitList;
	}

	@Override
	public List<BaseConstructionPermit> selectBaseConstructionPermitByUnitEngId(String unitEngineeringInfoGuid) {
		List<BaseConstructionPermit> bcpList = constructionPermitMapper.selectBaseConstructionPermitByUnitEngId(unitEngineeringInfoGuid);
		if(!StringUtils.isEmpty(bcpList)) {
			BaseConstructionPermit bcp = bcpList.get(0);
			bcp.setTotalCompany(FieldStrUtils.personOrEnterFieldNot(bcp.getTotalCompany()));
			if(StrUtil.containsAnyIgnoreCase(bcp.getConsUnitResponsePerson(),"|")) {
				bcp.setConsUnitResponsePerson(FieldStrUtils.personOrEnterFieldNot(bcp.getConsUnitResponsePerson()));
			}
		}
		return bcpList;
	}

	@Override
	public String selectBaseConstructionPermitStateByUnitEngId(String unitEngineeringInfoGuid) {
		// TODO Auto-generated method stub
		return constructionPermitMapper.selectBaseConstructionPermitStateByUnitEngId(unitEngineeringInfoGuid);
	}

	@Override
	public BaseConstructionPermit selectConsturctionLicenceInfoByGuid(String guid) {
		// TODO Auto-generated method stub
		return constructionPermitMapper.selectConsturctionLicenceInfoByGuid(guid);
	}

	@Override
	public ConstructionPermitUnit selectConsturctionLicenceUnit(String dataGuid,BaseConstructionPermit bcp) {
		ConstructionPermitUnit cpu = new ConstructionPermitUnit();
		cpu.setConstructionUnitGuid(constructionPermitMapper.selectConstructionUnitGuid(dataGuid));
		cpu.setConstructionUnitName(constructionPermitMapper.selectConstructionUnitName(dataGuid));
		cpu.setConstructionQuaLv(constructionPermitMapper.selectConstructionQuaLv(dataGuid));
		cpu.setConstructionAddress(constructionPermitMapper.selectConstructionAddress(dataGuid));
		cpu.setConstructionPhone(constructionPermitMapper.selectConstructionPhone(dataGuid));
		cpu.setConstructionLegal(constructionPermitMapper.selectConstructionLegal(dataGuid));
		cpu.setConstructionLegalCardId(constructionPermitMapper.selectConstructionLegalCardId(dataGuid));
		cpu.setConstructionLegalPhone(constructionPermitMapper.selectConstructionLegalPhone(dataGuid));
		cpu.setConstructionLeader(constructionPermitMapper.selectConstructionLeader(dataGuid));
		cpu.setConstructionLeaderCardId(constructionPermitMapper.selectConstructionLeaderCardId(dataGuid));
		cpu.setConstructionLeaderPhone(constructionPermitMapper.selectConstructionLeaderPhone(dataGuid));
		
		cpu.setAgentConstructionUnitGuid(constructionPermitMapper.selectAgentConstructionUnitGuid(dataGuid));
		cpu.setAgentConstructionUnitName(constructionPermitMapper.selectAgentConstructionUnitName(dataGuid));
		cpu.setAgentConstructionUnitQuaLv(constructionPermitMapper.selectAgentConstructionUnitQuaLv(dataGuid));
		cpu.setAgentConstructionUnitLeader(constructionPermitMapper.selectAgentConstructionUnitLeader(dataGuid));
		cpu.setAgentConstructionUnitLeaderCardId(constructionPermitMapper.selectAgentConstructionUnitLeaderCardId(dataGuid));
		cpu.setAgentConstructionUnitLeaderPhone(constructionPermitMapper.selectAgentConstructionUnitLeaderPhone(dataGuid));
		
		cpu.setSurveyUnitGuid(constructionPermitMapper.selectSurveyUnitGuid(dataGuid));
		cpu.setSurveyUnitName(constructionPermitMapper.selectSurveyUnitName(dataGuid));
		cpu.setSurveyUnitQuaLv(constructionPermitMapper.selectSurveyUnitQuaLv(dataGuid));
		cpu.setSurveyUnitLeader(constructionPermitMapper.selectSurveyUnitLeader(dataGuid));
		cpu.setSurveyUnitLeaderCardId(constructionPermitMapper.selectSurveyUnitLeaderCardId(dataGuid));
		cpu.setSurveyUnitLeaderPhone(constructionPermitMapper.selectSurveyUnitLeaderPhone(dataGuid));
		cpu.setSurveyUnitLeaderZYZG(constructionPermitMapper.selectSurveyUnitLeaderZYZG(dataGuid));
		
		cpu.setDesignUnitGuid(constructionPermitMapper.selectDesignUnitGuid(dataGuid));
		cpu.setDesignUnitName(constructionPermitMapper.selectDesignUnitName(dataGuid));
		cpu.setDesignUnitQuaLv(constructionPermitMapper.selectDesignUnitQuaLv(dataGuid));
		cpu.setDesignUnitLeader(constructionPermitMapper.selectDesignUnitLeader(dataGuid));
		cpu.setDesignUnitLeaderCardId(constructionPermitMapper.selectDesignUnitLeaderCardId(dataGuid));
		cpu.setDesignUnitLeaderPhone(constructionPermitMapper.selectDesignUnitLeaderPhone(dataGuid));
		cpu.setDesignUnitLeaderZYZG(constructionPermitMapper.selectDesignUnitLeaderZYZG(dataGuid));
		
		cpu.setGeneralContractUnitGuid(constructionPermitMapper.selectGeneralContractUnitGuid(dataGuid));
		cpu.setGeneralContractUnitName(constructionPermitMapper.selectGeneralContractUnitName(dataGuid));
		cpu.setGeneralContractUnitQuaLv(constructionPermitMapper.selectGeneralContractUnitQuaLv(dataGuid));
		cpu.setGeneralContractUnitLeader(constructionPermitMapper.selectGeneralContractUnitLeader(dataGuid));
		cpu.setGeneralContractUnitLeaderCardId(constructionPermitMapper.selectGeneralContractUnitLeaderCardId(dataGuid));
		cpu.setGeneralContractUnitLeaderPhone(constructionPermitMapper.selectGeneralContractUnitLeaderPhone(dataGuid));
		cpu.setGeneralContractUnitLeaderZYZG(constructionPermitMapper.selectGeneralContractUnitLeaderZYZG(dataGuid));
		
		cpu.setConstructUnitGuid(constructionPermitMapper.selectConstructUnitGuid(dataGuid));
		cpu.setConstructUnitName(constructionPermitMapper.selectConstructUnitName(dataGuid));
		cpu.setConstructUnitQuaLv(constructionPermitMapper.selectConstructUnitQuaLv(dataGuid));
		cpu.setConstructUnitLeader(constructionPermitMapper.selectConstructUnitLeader(dataGuid));
		cpu.setConstructUnitLeaderCardId(constructionPermitMapper.selectConstructUnitLeaderCardId(dataGuid));
		cpu.setConstructUnitLeaderPhone(constructionPermitMapper.selectConstructUnitLeaderPhone(dataGuid));
		cpu.setConstructUnitLeaderZYZG(constructionPermitMapper.selectConstructUnitLeaderZYZG(dataGuid));
		cpu.setConstructUnitValidity(constructionPermitMapper.selectConstructUnitValidity(dataGuid));
		
		cpu.setSupervisorUnitGuid(constructionPermitMapper.selectSupervisorUnitGuid(dataGuid));
		cpu.setSupervisorUnitName(constructionPermitMapper.selectSupervisorUnitName(dataGuid));
		cpu.setSupervisorUnitQuaLv(constructionPermitMapper.selectSupervisorUnitQuaLv(dataGuid));
		cpu.setSupervisorUnitZJLGCS(constructionPermitMapper.selectSupervisorUnitZJLGCS(dataGuid));
		cpu.setSupervisorUnitZJLGCSCardId(constructionPermitMapper.selectSupervisorUnitZJLGCSCardId(dataGuid));
		cpu.setSupervisorUnitZJLGCSPhone(constructionPermitMapper.selectSupervisorUnitZJLGCSPhone(dataGuid));
		cpu.setSupervisorUnitZJLGCSZYZG(constructionPermitMapper.selectSupervisorUnitZJLGCSZYZG(dataGuid));
		
		if(bcp!=null) {
			if(!StringUtils.isEmpty(bcp.getConstructUnitId())) {
				cpu.setConstructionUnitGuid(bcp.getConstructUnitId());
			}
			if(!StringUtils.isEmpty(bcp.getConstructUnit())) {
				cpu.setConstructionUnitName(bcp.getConstructUnit());
			}
			if(!StringUtils.isEmpty(bcp.getSurveyUnitName())) {
				if(!StringUtils.isEmpty(FieldStrUtils.personOrEnterFieldParaNot(bcp.getSurveyUnitName(), 2, 0))) {
					cpu.setSurveyUnitGuid(FieldStrUtils.personOrEnterFieldParaNot(bcp.getSurveyUnitName(), 2, 0));
				}
				if(!StringUtils.isEmpty(FieldStrUtils.personOrEnterFieldParaNot(bcp.getSurveyUnitName(), 2, 1))) {
					cpu.setSurveyUnitName(FieldStrUtils.personOrEnterFieldParaNot(bcp.getSurveyUnitName(), 2, 1));
				}
			}
			if(!StringUtils.isEmpty(bcp.getDesignUnit())) {
				if(!StringUtils.isEmpty(FieldStrUtils.personOrEnterFieldParaNot(bcp.getDesignUnit(),2,0))) {
					cpu.setDesignUnitGuid(FieldStrUtils.personOrEnterFieldParaNot(bcp.getDesignUnit(),2,0));
				}
				if(!StringUtils.isEmpty(FieldStrUtils.personOrEnterFieldParaNot(bcp.getDesignUnit(),2,1))) {
					cpu.setDesignUnitName(FieldStrUtils.personOrEnterFieldParaNot(bcp.getDesignUnit(),2,1));
				}
			}
			if(!StringUtils.isEmpty(bcp.getTotalCompany())) {
				if(!StringUtils.isEmpty(FieldStrUtils.personOrEnterFieldParaNot(bcp.getTotalCompany(),2,0))) {
					cpu.setConstructUnitGuid(FieldStrUtils.personOrEnterFieldParaNot(bcp.getTotalCompany(),2,0));
				}
				if(!StringUtils.isEmpty(FieldStrUtils.personOrEnterFieldParaNot(bcp.getTotalCompany(),2,1))) {
					cpu.setConstructUnitName(FieldStrUtils.personOrEnterFieldParaNot(bcp.getTotalCompany(),2,1));
				}
			}
			if(!StringUtils.isEmpty(bcp.getSupervisionUnit())) {
				if(!StringUtils.isEmpty(FieldStrUtils.personOrEnterFieldParaNot(bcp.getSupervisionUnit(),2,0))) {
					cpu.setSupervisorUnitGuid(FieldStrUtils.personOrEnterFieldParaNot(bcp.getSupervisionUnit(),2,0));
				}
				if(!StringUtils.isEmpty(FieldStrUtils.personOrEnterFieldParaNot(bcp.getSupervisionUnit(),2,1))) {
					cpu.setSupervisorUnitName(FieldStrUtils.personOrEnterFieldParaNot(bcp.getSupervisionUnit(),2,1));
				}
			}
		}
		return cpu;
	}

	@Override
	public List<ResponsiblePersonInfo> selectRespPerByUnitTypeAndDataGuid(ResponsiblePersonInfo rpi) {
		// TODO Auto-generated method stub
		return constructionPermitMapper.selectRespPerByUnitTypeAndDataGuid(rpi);
	}

	@Override
	public List<SafetyMeasuresInfo> selectSafetyMeasuresInfoByDataGuid(String dataGuid) {
		// TODO Auto-generated method stub
		return constructionPermitMapper.selectSafetyMeasuresInfoByDataGuid(dataGuid);
	}

	@Override
	public List<ConPermitFile> selectConPermitFileByObjectId(String objectId) {
		// TODO Auto-generated method stub
		return constructionPermitMapper.selectConPermitFileByObjectId(objectId);
	}

	@Override
	public String selectOperateDescription(String objectId) {
		List<String> operateDescriptionList = constructionPermitMapper.selectOperateDescription(objectId);
		if(!StringUtils.isEmpty(operateDescriptionList)) {
			return operateDescriptionList.get(0);
		}
		return null;
	}

}
